package com.corpcredit.webapp.action;

import java.io.Serializable;

import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Sb;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class SbForm extends BasePage implements Serializable {
    private GenericManager<Sb, Long> sbManager;
    private Sb sb = new Sb();
    private Long id;
    
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }
    public void setSbManager(GenericManager<Sb, Long> sbManager) {
        this.sbManager = sbManager;
    }

    public Sb getSb() {
        return sb;
    }

    public void setSb(Sb sb) {
        this.sb = sb;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        sbManager.remove(sb.getId());
        addMessage("sb.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            sb = sbManager.get(id);
        } else {
            sb = new Sb();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (sb.getId() == null || sb.getId() == 0);
        // set the related jbqk as a foreign key.
        sb.setJbqk( jbqk );
        sbManager.save(sb);

        String key = (isNew) ? "sb.added" : "sb.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 